package com.bm.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bm.server.config.RedisKeyConfig;
import com.bm.server.dto.UserLoginDto;
import com.bm.server.type.UserLogType;
import com.bm.server.util.JedisUtil;
import com.bm.server.util.Rutils;
import com.bm.server.util.StringUtil;
import com.bm.server.vo.R;
import com.bm.server.dao.UserDao;
import com.bm.server.dao.UserLogDao;
import com.bm.server.entity.User;
import com.bm.server.entity.UserLog;
import com.bm.server.service.intf.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao dao;
    @Resource
    private UserLogDao logDao;
    //手机号正则校验
    @Override
    public R checkPhone(String phone) {
        System.err.println(phone);
        if (StringUtil.isPhone(phone)) {
//            QueryWrapper<User> wrapper = new QueryWrapper<>();
//            wrapper.eq("phone", phone);
//            User user = dao.selectOne(wrapper);
//            if (user != null) {
                return Rutils.ok();
            }
        
        return Rutils.fail("亲请检查你得手机号哦");
    }
    
    /*@Override
    public R phoneLogin(UserLoginDto dto) {
        if(dto!=null){
            //支持手机号登陆
            if(StringUtil.isNotEmpty(dto.getPhone(),dto.getCode())){
                QueryWrapper<User> wrapper = new QueryWrapper<>();
                wrapper.eq("phone",dto.getPhone());
                User user = dao.selectOne(wrapper);
                //验证手机号是否存在
                if (user != null) {
                    if(dto.getCode().equals(JedisUtil.getString(RedisKeyConfig.SMS_CODE_LOGIN+dto.getCode()))){
                            if (JedisUtil.checkKey(RedisKeyConfig.LOGIN_UID+user.getUid())){
                                return Rutils.fail("亲亲，账号已经在线了哦,无法重复登陆哦");
                            }else{
                                JedisUtil.addString(RedisKeyConfig.LOGIN_UID+user.getUid(), JSON.toJSONString(user),RedisKeyConfig.LOGIN_UID_TIME);
                                logDao.insert(new UserLog(user.getUid(), UserLogType.登录.getValue(),"完成了手机验证码登录", dto.getCity(), dto.getMobie()));
                                   return Rutils.ok(user.getUid());
                                }
                    }
                }
            }
            
        }
        return Rutils.fail("亲亲账号或密码有误哦");
    }*/
    // 验证码登陆
        @Override
        public R phoneLogin(UserLoginDto dto) {
            if(dto!=null){
                //支持手机号登陆
                if(StringUtil.isNotEmpty(dto.getPhone(),dto.getCode())){
                    QueryWrapper<User> wrapper = new QueryWrapper<>();
                    wrapper.eq("phone",dto.getPhone());
                    User user = dao.selectOne(wrapper);
                    //验证手机号是否存在
                    if (user != null) {
                        if(dto.getCode().equals(JedisUtil.getString(RedisKeyConfig.SMS_CODE_LOGIN+dto.getPhone()))){
                            if (JedisUtil.checkKey(RedisKeyConfig.LOGIN_UID+user.getUid())){
                                return Rutils.fail("亲亲，账号已经在线了哦,无法重复登陆哦");
                            }else{      
                                JedisUtil.addString(RedisKeyConfig.LOGIN_UID+user.getUid(), JSON.toJSONString(user),RedisKeyConfig.LOGIN_UID_TIME);
                                logDao.insert(new UserLog(user.getUid(), UserLogType.登录.getValue(),"完成了手机验证码登录", dto.getCity(), dto.getMobie()));
                                return Rutils.ok(user.getUid());
                            }
                        }
                    }else {
                        //验证短信验证码
                        if (dto.getCode().equals(JedisUtil.getString(RedisKeyConfig.SMS_CODE_LOGIN+dto.getPhone()))){
                            //没有·手机号，开始注册
                            User user1 = new User();
                            user1.setPhone(dto.getPhone());
                            System.out.println(user1.getPhone());
                            if (dao.insert(user1)>0){
                                return Rutils.ok("哥们，哥们你敢相信你登上了！！！载入史册");
                            }
                        }
                    }
                }

            }
            return Rutils.fail("亲亲账号或验证码有误哦");
        }
    //注销
    @Override
    public R loginOut(int uid) {
            JedisUtil.delKeys(RedisKeyConfig.LOGIN_UID+uid);
                    return Rutils.ok();
            }
    //一键登录
    @Override
    public R oauth(String phone) {
        if(StringUtil.isPhone(phone)){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",phone);
            User user = dao.selectOne(wrapper);
            if(user!=null){
               if(JedisUtil.checkKey(RedisKeyConfig.LOGIN_UID+user.getUid())){
                   return Rutils.fail("兄弟，你号被别人登啦，还不问问谁登录的");
               }else{
                   JedisUtil.addString(RedisKeyConfig.LOGIN_UID+user.getUid(), JSON.toJSONString(user),RedisKeyConfig.LOGIN_UID_TIME);
                   return Rutils.ok("恭喜你这个b成功登录了");
               }
            }else{
                User user1 = new User();
                user1.setPhone(phone);
                if (dao.insert(user1)>0){
                    JedisUtil.addString(RedisKeyConfig.LOGIN_UID+user.getUid(), JSON.toJSONString(user),RedisKeyConfig.LOGIN_UID_TIME);
                    return Rutils.ok("芜湖~不错呀还能登上");
                }
            }
            
        }return Rutils.fail("铁子，check your phoneNumber");
    }

}